﻿using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Read.API.Applications.Dtos;
using B.S.Business.Read.API.Applications.Export;
using MediatR;
using System.Collections.Generic;

namespace B.S.Business.Read.API.Applications.CommandHandler.ExportCommandHandler
{
    /// <summary>
    /// 导出
    /// </summary>
    public class ExportQuserCommandHandler : IRequestHandler<ExportQuserCommand, ApiResult<List<ExportCommodityDto>>>
    {
        private readonly IBaseRepository<Purchase> repository;

        public ExportQuserCommandHandler(IBaseRepository<Purchase> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<ExportCommodityDto>>> Handle(ExportQuserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ExportCommodityDto>> result = new ApiResult<List<ExportCommodityDto>>();

            var list = from a in repository.GetAll()
                       where request.PurchaseId.Contains(a.PurchaseId)
                       select new ExportCommodityDto
                       {
                          PurchaseId = a.PurchaseId,
                           ProductName = a.ProductName,
                           Productcode = a.Productcode,
                           Specification = a.Specification,
                           UnitName = a.UnitName,
                           Univalence = a.Univalence,
                           Quantity = a.Quantity,
                           ExtendedPrice = a.ExtendedPrice,
                           IsDeleted = a.IsDeleted
                       };

            result.Code = ApiCode.查询成功;
            result.Msg = ApiCode.查询成功.ToString();
            result.Data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
